package PrjDBUtils

import (
	"dgo/goutils/utils"
	"errors"
	"flag"
	"fmt"
	"gitee.com/ymofen/gobase/golog"
	"gitee.com/ymofen/supervalue"
)

var (
	datasourceconfigfile *string
)

func CmdFlagPrepareDataSource() {
	datasourceconfigfile = flag.String("datasourceconfig", "", "")
}

func LoadDataSourceConfigFromFile(filename string) (int, error) {
	if utils.FileIsExists(filename) {
		json, err := wrapper.NewSVFromFile(filename)
		if err != nil {
			return 0, err
		}
		r := LoadDataSourceConfig(json.ValueByName("datasource"))
		utils.Log.Infof("加载数据源:%d,配置文件[%s]", r, filename)
		return r, nil
	} else {
		return 0, errors.New(fmt.Sprintf("配置文件(%s)不存在.", filename))
	}

}

func LoadDataSourceConfigFromStartupParams() (int, error) {
	if datasourceconfigfile != nil {
		return LoadDataSourceConfigFromFile(*datasourceconfigfile)
	} else {
		return 0, nil
	}
}

func LoadDataSourceConfigFromAnywhere() (int, error) {
	if datasourceconfigfile != nil {
		return LoadDataSourceConfigFromFile(*datasourceconfigfile)
	} else {
		strName := "datasource.config.json"
		filename := utils.GetExecuteFileDirectory()
		filename += utils.PathSeparator + strName
		if !utils.FileIsExists(filename) {
			filename = utils.GetExecuteFileDirectory() + utils.PathSeparator + "config" + utils.PathSeparator + strName
			if !utils.FileIsExists(filename) {
				filename = strName
				if !utils.FileIsExists(filename) {
					filename = "config" + utils.PathSeparator + strName
				}
			}
		}

		if !utils.FileIsExists(filename) {
			return 0, errors.New(fmt.Sprintf("找不到对应的配置文件[%s].", strName))
		}
		return LoadDataSourceConfigFromFile(filename)
	}
}

func LoadDataSourceConfigObject(config *wrapper.SuperValue, id string) int {
	if config == nil {
		return 0
	}

	itm := config
	if len(id) == 0 {
		id = itm.AsString("id", "")
	}
	str := itm.AsString("connstring", "")
	driverName := itm.AsString("drivername", "mysql")
	if len(id) > 0 {
		ConfigDatasourceAll(id, str, driverName, itm.StringByName("dbtype", driverName),
			itm.StringByName("field-prefix", ""), itm.StringByName("field-suffix", ""))
		golog.Debugf("加载数据源配置[%s:%s]", id, str)
		return 1
	} else {
		utils.Log.Warnf("加载失败%s", itm.ToString())
		return 0
	}

}

/*
*

	加载配置
	[{"id":"main", connstring:"xxxxx"},{}]
*/
func LoadDataSourceConfig(config *wrapper.SuperValue) int {
	if config == nil {
		return 0
	}
	succ := 0
	for i := 0; i < config.Length(); i++ {
		itm := config.AsRecord(i)
		id := itm.AsString("id", "")
		str := itm.AsString("connstring", "")
		driverName := itm.AsString("drivername", "mysql")
		if len(id) > 0 {
			ConfigDatasourceAll(id, str, driverName, itm.StringByName("dbtype", driverName),
				itm.StringByName("field-prefix", ""), itm.StringByName("field-suffix", ""))
			golog.Debugf("加载数据源配置[%s:%s]", id, str)
			succ++
		} else {
			utils.Log.Warnf("加载失败%s", itm.ToString())
		}
	}

	return succ
}

///**
//  加载配置
//  [{"id":"main", connstring:"xxxxx"},{}]
//*/
//func LoadDataSourceConfig2(config *wrapper.SuperValue) int {
//	if config == nil {
//		return 0
//	}
//	locker.Lock()
//	defer locker.Unlock()
//	succ := 0
//	for i := 0; i < config.Count(); i++ {
//		itm := config.ValueByIndex(i)
//		id := itm.AsString("id", "")
//		str := itm.AsString("connstring", "")
//		driverName := itm.AsString("drivername", "mysql")
//		if len(id) > 0 {
//			configMap[id] = &configrec{id, str, driverName}
//			golog.Debugf("加载数据源配置[%s:%s]", id, str)
//			succ++
//		} else {
//			utils.Log.Warnf("加载失败%s", itm.StatusString())
//		}
//	}
//	return succ
//}
